<?php 

defined('G_IN_SYSTEM')or exit('No permission resources.');

System::load_app_class('base','member','no');

System::load_app_fun('user','go');

class cart extends base {

	private $Cartlist;

	public function __construct() {			

		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 

		header("Cache-Control: no-cache, must-revalidate" ); 

		header("Pragma:no-cache");

		

		$this->Cartlist = _getcookie('Cartlist');

		$this->db = System::load_sys_class("model");

	}



	//获取购物车的商品信息到头部

	public function cartheader(){

		$Cartlist=json_decode(stripslashes($this->Cartlist),true);	

		$shopids='';	

		if(is_array($Cartlist)){			

			foreach($Cartlist as $key => $val){

				$shopids.=intval($key).',';				

			}

			$shopids=str_replace(',0','',$shopids);

			$shopids=trim($shopids,',');

		}		

		$shoplist=array();

		if($shopids!=NULL){

			$shoplist=$this->db->GetList("SELECT * FROM `@#_shoplist` where `id` in($shopids) limit 5",array("key"=>"id"));

		}

		$MoenyCount=0;

		$Cartshopinfo='{';

		if(count($shoplist)>=1){

			foreach($Cartlist as $key => $val){

					$key=intval($key);					

					if(isset($shoplist[$key])){									

						$shoplist[$key]['cart_gorenci']=$val['num'] ? $val['num'] : 1;						

						$MoenyCount+=$shoplist[$key]['yunjiage']*$shoplist[$key]['cart_gorenci'];

						$shoplist[$key]['cart_xiaoji']=substr(sprintf("%.3f",$shoplist[$key]['yunjiage']*$val['num']),0,-1);					

						$shoplist[$key]['cart_shenyu']=$shoplist[$key]['zongrenshu']-$shoplist[$key]['canyurenshu'];

						$Cartshopinfo.="'$key':{'shenyu':".$shoplist[$key]['cart_shenyu'].",'num':".$val['num'].",'money':".$shoplist[$key]['yunjiage']."},";

					}

			}

		}		

		$MoenyCount=substr(sprintf("%.3f",$MoenyCount),0,-1);	

		$Cartshopinfo.="'MoenyCount':$MoenyCount}";		

		

		if(!is_array($Cartlist)){

			$shop['num']=0;

		}else{

			$shop['num']=count($Cartlist)-1;		

		}	

	

		$shop['sum']=$MoenyCount;

		echo json_encode($shop);

	}

	//获取购物车的商品信息

	public function cartshop(){		

		$Cartlist=json_decode(stripslashes($this->Cartlist),true);	

		$shopids='';	

		if(is_array($Cartlist)){			

			foreach($Cartlist as $key => $val){

				$shopids.=intval($key).',';				

			}

			$shopids=str_replace(',0','',$shopids);

			$shopids=trim($shopids,',');	

		}		

		$shoplist=array();

		if($shopids!=NULL){

			$shoplist=$this->db->GetList("SELECT * FROM `@#_shoplist` where `id` in($shopids) limit 7",array("key"=>"id"));

		}	

		

		$MoenyCount=0;

		$Cartshopinfo='{';

		if(count($shoplist)>=1){

			foreach($Cartlist as $key => $val){

					$key=intval($key);					

					if(isset($shoplist[$key])){									

						$shoplist[$key]['cart_gorenci']=$val['num'] ? $val['num'] : 1;						

						$MoenyCount+=$shoplist[$key]['yunjiage']*$shoplist[$key]['cart_gorenci'];

						$shoplist[$key]['cart_xiaoji']=substr(sprintf("%.3f",$shoplist[$key]['yunjiage']*$val['num']),0,-1);					

						$shoplist[$key]['cart_shenyu']=$shoplist[$key]['zongrenshu']-$shoplist[$key]['canyurenshu'];

						$Cartshopinfo.="'$key':{'shenyu':".$shoplist[$key]['cart_shenyu'].",'num':".$val['num'].",'money':".$shoplist[$key]['yunjiage']."},";

					}

			}

		}		

		$MoenyCount=substr(sprintf("%.3f",$MoenyCount),0,-1);	

		$Cartshopinfo.="'MoenyCount':$MoenyCount}";		



		$li="";

		foreach($shoplist as $st){			

			$li.='<li id="shopid'.$st['id'].'">';

			$li.='<a href="javascript:;" onclick="delshop('.$st['id'].')" title="删除" class="Close"></a>';

			$li.='<a href="'.WEB_PATH.'/goods/'.$st['id'].'"><img src="'.G_UPLOAD_PATH.'/'.$st['thumb'].'" title="'.$st['title'].'"></a>';

			$li.='<span class="orange">'.$st['cart_gorenci'].'</span>人次';

			$li.='</li>';

		}

		if(count($shoplist)>=7){

			$li.='<li class="Roll_CartMore"><a target="_blank" title="查看更多" href="'.WEB_PATH.'/member/cart/cartlist">更多<i>&gt;</i></a></li>';

		}

		

		$shop['li']=$li;

		if(!is_array($Cartlist)){

			$shop['num']=0;

		}else{

			$shop['num']=count($Cartlist)-1;		

		}	

		$shop['sum']=$MoenyCount;

		echo json_encode($shop);

	}

	//获取购物车的商品数量ajax

	public function getnumber(){		

		$Cartlist=json_decode(stripslashes($this->Cartlist),true);

		if(!is_array($Cartlist)){

			echo 0;

		}else{

			echo count($Cartlist)-1;		

		}

	}

	

	//购物车商品列表

	public function cartlist(){	

		$Cartlist=json_decode(stripslashes($this->Cartlist),true);		

		$shopids='';	

		if(is_array($Cartlist)){			

			foreach($Cartlist as $key => $val){

				$shopids.=intval($key).',';				

			}

			$shopids=str_replace(',0','',$shopids);

			$shopids=trim($shopids,',');

	

		}

		$shoplist=array();

		if($shopids!=NULL){

			$shoplist=$this->db->GetList("SELECT * FROM `@#_shoplist` where `id` in($shopids)",array("key"=>"id"));

		}

		$MoenyCount=0;

		$Cartshopinfo='{';

		if(count($shoplist)>=1){

			foreach($Cartlist as $key => $val){

					$key=intval($key);					

					if(isset($shoplist[$key])){									

						$shoplist[$key]['cart_gorenci']=$val['num'] ? $val['num'] : 1;						

						$MoenyCount+=$shoplist[$key]['yunjiage']*$shoplist[$key]['cart_gorenci'];

						$shoplist[$key]['cart_xiaoji']=substr(sprintf("%.3f",$shoplist[$key]['yunjiage']*$val['num']),0,-1);					

						$shoplist[$key]['cart_shenyu']=$shoplist[$key]['zongrenshu']-$shoplist[$key]['canyurenshu'];

						$Cartshopinfo.="'$key':{'shenyu':".$shoplist[$key]['cart_shenyu'].",'num':".$val['num'].",'money':".$shoplist[$key]['yunjiage']."},";

					}

			}

		}

		$MoenyCount=substr(sprintf("%.3f",$MoenyCount),0,-1);	

		$Cartshopinfo.="'MoenyCount':$MoenyCount}";		

		include templates("cart","cartlist");

	}

	//支付界面

	public function pay(){



		parent::__construct();	

		if(!$member=$this->userinfo)$this->HeaderLogin();				

		

		$Cartlist=json_decode(stripslashes($this->Cartlist),true);

		

		

			

		$shopids='';	

		if(is_array($Cartlist)){			

			foreach($Cartlist as $key => $val){

				$shopids.=intval($key).',';				

			}

			$shopids=str_replace(',0','',$shopids);

			$shopids=trim($shopids,',');

	

		}

		$shoplist=array();

		if($shopids!=NULL){

			$shoplist=$this->db->GetList("SELECT * FROM `@#_shoplist` where `id` in($shopids)",array("key"=>"id"));

		}

		$MoenyCount=0;

		if(count($shoplist)>=1){

			foreach($Cartlist as $key => $val){

					$key=intval($key);					

					if(isset($shoplist[$key])){		

						$shoplist[$key]['cart_gorenci']=$val['num'] ? $val['num'] : 1;							

						$MoenyCount+=$shoplist[$key]['yunjiage']*$shoplist[$key]['cart_gorenci'];

						$shoplist[$key]['cart_xiaoji']=substr(sprintf("%.3f",$shoplist[$key]['yunjiage']*$val['num']),0,-1);					

						$shoplist[$key]['cart_shenyu']=$shoplist[$key]['zongrenshu']-$shoplist[$key]['canyurenshu'];					

					}

			}

		}else{

			_setcookie('Cartlist',NULL);

			_message("购物车没有商品!",WEB_PATH);

		}	

		

		//总支付价格

		$MoenyCount=substr(sprintf("%.3f",$MoenyCount),0,-1);

		//会员余额

		$Money=$member['money'];

		//商品数量

		$shoplen=count($shoplist);		

		

		$fufen = System::load_app_config("user_fufen");

		if($fufen['fufen_yuan']){

			$fufen_dikou = intval($member['score'] / $fufen['fufen_yuan']);

		}else{

			$fufen_dikou = 0;

		}

		$paylist = $this->db->GetList("SELECT * from `@#_pay` where `pay_start` = '1' AND `pay_mobile` != 1 ");

		$cookies = base64_encode($this->Cartlist);

		

		session_start();

		$_SESSION['submitcode'] = $submitcode = uniqid();

		

		include templates("cart","pay");

	}

	

	//开始支付

	public function paysubmit(){

		if(!isset($_POST['submit'])){	

			_message("正在返回购物车...",WEB_PATH.'/member/cart/cartlist');

			exit;

		}	

		

		session_start();		

		if(isset($_POST['submitcode'])) {

			if(isset($_SESSION['submitcode'])){

				$submitcode = $_SESSION['submitcode'];

			}else{

				$submitcode = null;

			}		

			if($_POST['submitcode'] == $submitcode){			

				unset($_SESSION["submitcode"]);      

			}else{				

				_message("请不要重复提交...",WEB_PATH.'/member/cart/cartlist');

			}	

		}else{

			_message("正在返回购物车...",WEB_PATH.'/member/cart/cartlist');

		}

	

	

		parent::__construct();	

		if(!$this->userinfo)$this->HeaderLogin();

		$uid = $this->userinfo['uid'];

		

				

		$pay_checkbox=isset($_POST['moneycheckbox']) ? true : false;	

		$pay_type_bank=isset($_POST['pay_bank']) ? $_POST['pay_bank'] : false;

		$pay_type_id=isset($_POST['account']) ? $_POST['account'] : false;

		

		

		if(isset($_POST['shop_score'])){

			$fufen_cfg = System::load_app_config("user_fufen",'','member');	

			$fufen = intval($_POST['shop_score_num']);			

			if($fufen_cfg['fufen_yuan']){				

				$fufen = intval($fufen / $fufen_cfg['fufen_yuan']);

				$fufen = $fufen * $fufen_cfg['fufen_yuan'];

			}			

		}else{

			$fufen = 0;

		}		

		/************start*************/

		$Cartlist=json_decode(stripslashes($this->Cartlist),true);

		$pay=System::load_app_class('pay','pay');

		//$pay->scookie = json_decode(base64_decode($_POST['cookies']));

			

		$pay->fufen = $fufen;

		$pay->pay_type_bank = $pay_type_bank;

		$ok = $pay->init($uid,$pay_type_id,'go_record');	//夺宝商品	



		if($ok !== 'ok'){

			$_COOKIE['Cartlist'] = NULL;

			_setcookie("Cartlist",null);

			_message($ok,G_WEB_PATH);

		}

		$check = $pay->go_pay($pay_checkbox);

		if($check === 'not_pay'){

			_message('未选择支付平台!',WEB_PATH.'/member/cart/cartlist');

		}

		if(!$check){

			_message("请配置支付信息!",WEB_PATH.'/member/cart/cartlist');

		}

		if($check){

			//成功

			header("location: ".WEB_PATH."/member/cart/paysuccess");

		}else{

			//失败	

			$_COOKIE['Cartlist'] = NULL;

			_setcookie("Cartlist",null);

			header("location: ".WEB_PATH);

		}		

		exit;

	}

	

	//成功页面

	public function paysuccess(){	

		$_COOKIE['Cartlist'] = NULL;

		_setcookie("Cartlist",null);

		include templates("cart","paysuccess");		

	}

	

	//充值

	public function addmoney(){

		parent::__construct();	

		if(!isset($_POST['submit'])){	

			_message("正在返回充值页面...",WEB_PATH.'/member/member/home/userrecharge');

			exit;

		}	

		if(!$this->userinfo)$this->HeaderLogin();		

		$pay_type_bank=isset($_POST['pay_bank']) ? $_POST['pay_bank'] : false;

		$pay_type_id=isset($_POST['account']) ? $_POST['account'] : false;		

		$money=intval($_POST['money']);		

		$uid = $this->userinfo['uid'];

		$pay=System::load_app_class('pay','pay');

		$pay->pay_type_bank = $pay_type_bank;

		$ok = $pay->init($uid,$pay_type_id,'addmoney_record',$money);

		if($ok === 'not_pay'){

			_message("未选择支付平台");

		}



		

	}



	//加入购物车异步获取信息

	public function Fastpay(){

		if($_GET['shopid']){

			$id = $_GET['shopid'];

			$shoplist=$this->db->GetOne("SELECT zongrenshu,canyurenshu,yunjiage as price, title as cg_title FROM `@#_shoplist` where `id` = $id");

			//用户账户金额

			$uid=_encrypt(_getcookie('uid'),'DECODE');

			if(empty($uid)){

				$shoplist['umoney'] = '未登录';

				$shoplist['ustatus'] ='0';

			}else{

				$money=$this->db->GetOne("SELECT `money` FROM `@#_member` where `uid` = $uid");

				$shoplist['umoney'] = $money['money'];

				$shoplist['ustatus'] ='1';

			}

			echo json_encode($shoplist);

		}

	}





	//快速异步支付信息

	public function Fastpaysubmit(){

		parent::__construct();	

				if(!$this->userinfo)$this->HeaderLogin();

				$uid = $this->userinfo['uid'];	

				$pay_checkbox= false;	

				$pay_type_bank= false;

				$pay_type_id= false;

				$var = $_POST;

				// 组合购物车数组

				$arr['shenyu'] = $var['shenyu'];

				$shopnum = $var['num'];

				$arr['money'] = $var['money'];

				$shopid = $var['shopid'];

				/************start*************/

				$pay=System::load_app_class('pay','pay');


				$rs  = $pay->pay_user_go_shop($uid,$shopid,$shopnum);

				if($rs){	//成功
						$message = array('error'=>'您已经支付成功');

						echo json_encode($message);
					exit;

		}

				

	}





}

